
#include <assert.h>
#include "app_platform/cmd_obj.h"
#include "app_platform/cmd_obj_ext.h"
#include "app_platform/cntl_info.h"
#include "app_platform/intf_service_control.h"
#include "acaccountdao_adaptor.h"
#include "acaccountdao_req_resp.h"


std::string g_sacaccountdaoStandAlone;
#include "app_platform/res_factory.h"
#include "c2cplatform/component/dal/dal_set.h"

using namespace c2cplatform::component::dal;

///////////////////////
// class CACAccountDaoSvcFactory
int CACAccountDaoSvcFactory::init(void* pvParam)
{
	IUserDefineCntl* pCntl = reinterpret_cast<IUserDefineCntl*>(pvParam);
	assert(pCntl);
		CDAL_SET->AddCmd("0xc0611805");
		CDAL_SET->AddCmd("0xc0611803");
		CDAL_SET->AddCmd("0xc0611806");
		CDAL_SET->AddCmd("0xc0611804");
		CDAL_SET->AddCmd("0xc0611808");
		CDAL_SET->AddCmd("0xc0611807");
		CDAL_SET->AddCmd("0xc0611802");
		CDAL_SET->AddCmd("0xc0611801");

	return CACAccountDao::Initialize(pCntl);
}

int CACAccountDaoSvcFactory::CXYZTimerHandler::OnTimer(int iTimerId)
{
    assert(0);
	return 0;
}

int CACAccountDaoSvcFactory::fini(void* pvParam)
{
    return 0;
}

uint32_t CACAccountDaoSvcFactory::getCateId() const
{   
	//方法已废弃
    return 0;
}

IService* CACAccountDaoSvcFactory::createService(uint32_t dwcmdId)
{
    return (new CACAccountDaoService(dwcmdId));
}

void CACAccountDaoSvcFactory::getServiceType(ServiceTypeMap_T& mapSvcType)
{
	mapSvcType[ACACCOUNTDAO_ADDANDUPDATEACITEMS_REQ_CMD_ID] = true;
	mapSvcType[ACACCOUNTDAO_ADDANDUPDATEBANKACCOUNT_REQ_CMD_ID] = true;
	mapSvcType[ACACCOUNTDAO_DELETEACITEMS_REQ_CMD_ID] = true;
	mapSvcType[ACACCOUNTDAO_DELETEBANKACCOUNT_REQ_CMD_ID] = true;
	mapSvcType[ACACCOUNTDAO_GETACITEMSBYFILTER_REQ_CMD_ID] = true;
	mapSvcType[ACACCOUNTDAO_GETACITEMSBYSYSNOS_REQ_CMD_ID] = true;
	mapSvcType[ACACCOUNTDAO_GETBANKACCOUNTBYFILTER_REQ_CMD_ID] = true;
	mapSvcType[ACACCOUNTDAO_GETBANKACCOUNTBYSYSNOS_REQ_CMD_ID] = true;
}

////////////////////////
// class CACAccountDaoService
CACAccountDaoService::CACAccountDaoService(uint32_t dwcmdId) 
: m_dwcmdId(dwcmdId)
{
    m_pSvc = new CACAccountDao;
}

void CACAccountDaoService::Reset() 
{ 
	m_pSvc -> Reset();
}
    
void CACAccountDaoService::getCmdIdRange(uint32_t& dwcmdIdBegin, uint32_t& dwcmdIdEnd) const 
{ 
    dwcmdIdBegin = m_dwcmdId;
    dwcmdIdEnd = m_dwcmdId;
} 
	
int CACAccountDaoService::Execute(CCmdObj& oCmd)
{
	switch(oCmd.getCmdId())
	{
	case ACACCOUNTDAO_ADDANDUPDATEACITEMS_REQ_CMD_ID:
        return AddAndUpdateACItems(oCmd);
	case ACACCOUNTDAO_ADDANDUPDATEBANKACCOUNT_REQ_CMD_ID:
        return AddAndUpdateBankAccount(oCmd);
	case ACACCOUNTDAO_DELETEACITEMS_REQ_CMD_ID:
        return DeleteACItems(oCmd);
	case ACACCOUNTDAO_DELETEBANKACCOUNT_REQ_CMD_ID:
        return DeleteBankAccount(oCmd);
	case ACACCOUNTDAO_GETACITEMSBYFILTER_REQ_CMD_ID:
        return GetACItemsByFilter(oCmd);
	case ACACCOUNTDAO_GETACITEMSBYSYSNOS_REQ_CMD_ID:
        return GetACItemsBySysNos(oCmd);
	case ACACCOUNTDAO_GETBANKACCOUNTBYFILTER_REQ_CMD_ID:
        return GetBankAccountByFilter(oCmd);
	case ACACCOUNTDAO_GETBANKACCOUNTBYSYSNOS_REQ_CMD_ID:
        return GetBankAccountBySysNos(oCmd);
	default:
		assert(0);
	}
	return 0;
}

int CACAccountDaoService::AddAndUpdateACItems(CCmdObj& oCmd)
{
	char* pBuf = NULL; uint32_t nLen = 0;
    oCmd.getBuffer(pBuf, nLen);
    CByteStreamNetwork bs(pBuf, nLen);
    bs.isStoring(false);
    bs.setVersion(oCmd.getVersion());
    CACAccountDaoAddAndUpdateACItemsReq oReq;
    if(!oReq.Serialize(bs))
    {
        m_sLastErrMsg = "Req.Serialize(bs) failed.";
        return -1;
    }

    CCntlInfo oCntlInfo;
    oCntlInfo.setCntl(reinterpret_cast<void*>(m_pSvcCntl));
    oCntlInfo.setOperatorUin(oCmd.getOperatorUin());
	oCntlInfo.setOperatorClientIP(oCmd.getOperatorClientIP());
	oCntlInfo.setOperatorKey(oCmd.getOperatorKey());
    oCntlInfo.setRouteKey(oCmd.getUin());
    oCntlInfo.setCmdId(oCmd.getCmdId());
    oCntlInfo.setNetioClientIP(oCmd.getNetioClientIP());
	oCntlInfo.setSerialNo(oCmd.getSerialNo());
	oCntlInfo.setPkgTimestamp(oCmd.getPkgTimestamp());
	
	if (oCmd.getVersion() == 3)
    {
        CCmdObjExt& oCmdExt = dynamic_cast<CCmdObjExt&>(oCmd);
        oCntlInfo.setAppId(oCmdExt.getReserve1());
        oCntlInfo.setDomainId(oCmdExt.getReserve2());
    }
	
	CACAccountDaoAddAndUpdateACItemsResp oResp;
	
    oResp.dwResult = m_pSvc->AddAndUpdateACItems(
		oCntlInfo,
		oReq.machineKey,
		oReq.source,
		oReq.sceneId,
		oReq.aCItemsDdo,
		oReq.inReserve,
		oReq.errmsg,
		oReq.aCItemsSysNo,
		oReq.outReserve
    );
	oResp.errmsg = oReq.errmsg;
	oResp.aCItemsSysNo = oReq.aCItemsSysNo;
	oResp.outReserve = oReq.outReserve;

    return m_pSvcCntl->putSvcObj(&oResp, oResp.dwResult);
}


int CACAccountDaoService::AddAndUpdateBankAccount(CCmdObj& oCmd)
{
	char* pBuf = NULL; uint32_t nLen = 0;
    oCmd.getBuffer(pBuf, nLen);
    CByteStreamNetwork bs(pBuf, nLen);
    bs.isStoring(false);
    bs.setVersion(oCmd.getVersion());
    CACAccountDaoAddAndUpdateBankAccountReq oReq;
    if(!oReq.Serialize(bs))
    {
        m_sLastErrMsg = "Req.Serialize(bs) failed.";
        return -1;
    }

    CCntlInfo oCntlInfo;
    oCntlInfo.setCntl(reinterpret_cast<void*>(m_pSvcCntl));
    oCntlInfo.setOperatorUin(oCmd.getOperatorUin());
	oCntlInfo.setOperatorClientIP(oCmd.getOperatorClientIP());
	oCntlInfo.setOperatorKey(oCmd.getOperatorKey());
    oCntlInfo.setRouteKey(oCmd.getUin());
    oCntlInfo.setCmdId(oCmd.getCmdId());
    oCntlInfo.setNetioClientIP(oCmd.getNetioClientIP());
	oCntlInfo.setSerialNo(oCmd.getSerialNo());
	oCntlInfo.setPkgTimestamp(oCmd.getPkgTimestamp());
	
	if (oCmd.getVersion() == 3)
    {
        CCmdObjExt& oCmdExt = dynamic_cast<CCmdObjExt&>(oCmd);
        oCntlInfo.setAppId(oCmdExt.getReserve1());
        oCntlInfo.setDomainId(oCmdExt.getReserve2());
    }
	
	CACAccountDaoAddAndUpdateBankAccountResp oResp;
	
    oResp.dwResult = m_pSvc->AddAndUpdateBankAccount(
		oCntlInfo,
		oReq.machineKey,
		oReq.source,
		oReq.sceneId,
		oReq.bankAccount,
		oReq.inReserve,
		oReq.errmsg,
		oReq.bankAccountSysNo,
		oReq.outReserve
    );
	oResp.errmsg = oReq.errmsg;
	oResp.bankAccountSysNo = oReq.bankAccountSysNo;
	oResp.outReserve = oReq.outReserve;

    return m_pSvcCntl->putSvcObj(&oResp, oResp.dwResult);
}


int CACAccountDaoService::DeleteACItems(CCmdObj& oCmd)
{
	char* pBuf = NULL; uint32_t nLen = 0;
    oCmd.getBuffer(pBuf, nLen);
    CByteStreamNetwork bs(pBuf, nLen);
    bs.isStoring(false);
    bs.setVersion(oCmd.getVersion());
    CACAccountDaoDeleteACItemsReq oReq;
    if(!oReq.Serialize(bs))
    {
        m_sLastErrMsg = "Req.Serialize(bs) failed.";
        return -1;
    }

    CCntlInfo oCntlInfo;
    oCntlInfo.setCntl(reinterpret_cast<void*>(m_pSvcCntl));
    oCntlInfo.setOperatorUin(oCmd.getOperatorUin());
	oCntlInfo.setOperatorClientIP(oCmd.getOperatorClientIP());
	oCntlInfo.setOperatorKey(oCmd.getOperatorKey());
    oCntlInfo.setRouteKey(oCmd.getUin());
    oCntlInfo.setCmdId(oCmd.getCmdId());
    oCntlInfo.setNetioClientIP(oCmd.getNetioClientIP());
	oCntlInfo.setSerialNo(oCmd.getSerialNo());
	oCntlInfo.setPkgTimestamp(oCmd.getPkgTimestamp());
	
	if (oCmd.getVersion() == 3)
    {
        CCmdObjExt& oCmdExt = dynamic_cast<CCmdObjExt&>(oCmd);
        oCntlInfo.setAppId(oCmdExt.getReserve1());
        oCntlInfo.setDomainId(oCmdExt.getReserve2());
    }
	
	CACAccountDaoDeleteACItemsResp oResp;
	
    oResp.dwResult = m_pSvc->DeleteACItems(
		oCntlInfo,
		oReq.machineKey,
		oReq.source,
		oReq.sceneId,
		oReq.ACItemsSysNo,
		oReq.inReserve,
		oReq.errmsg,
		oReq.outReserve
    );
	oResp.errmsg = oReq.errmsg;
	oResp.outReserve = oReq.outReserve;

    return m_pSvcCntl->putSvcObj(&oResp, oResp.dwResult);
}


int CACAccountDaoService::DeleteBankAccount(CCmdObj& oCmd)
{
	char* pBuf = NULL; uint32_t nLen = 0;
    oCmd.getBuffer(pBuf, nLen);
    CByteStreamNetwork bs(pBuf, nLen);
    bs.isStoring(false);
    bs.setVersion(oCmd.getVersion());
    CACAccountDaoDeleteBankAccountReq oReq;
    if(!oReq.Serialize(bs))
    {
        m_sLastErrMsg = "Req.Serialize(bs) failed.";
        return -1;
    }

    CCntlInfo oCntlInfo;
    oCntlInfo.setCntl(reinterpret_cast<void*>(m_pSvcCntl));
    oCntlInfo.setOperatorUin(oCmd.getOperatorUin());
	oCntlInfo.setOperatorClientIP(oCmd.getOperatorClientIP());
	oCntlInfo.setOperatorKey(oCmd.getOperatorKey());
    oCntlInfo.setRouteKey(oCmd.getUin());
    oCntlInfo.setCmdId(oCmd.getCmdId());
    oCntlInfo.setNetioClientIP(oCmd.getNetioClientIP());
	oCntlInfo.setSerialNo(oCmd.getSerialNo());
	oCntlInfo.setPkgTimestamp(oCmd.getPkgTimestamp());
	
	if (oCmd.getVersion() == 3)
    {
        CCmdObjExt& oCmdExt = dynamic_cast<CCmdObjExt&>(oCmd);
        oCntlInfo.setAppId(oCmdExt.getReserve1());
        oCntlInfo.setDomainId(oCmdExt.getReserve2());
    }
	
	CACAccountDaoDeleteBankAccountResp oResp;
	
    oResp.dwResult = m_pSvc->DeleteBankAccount(
		oCntlInfo,
		oReq.machineKey,
		oReq.source,
		oReq.sceneId,
		oReq.bankAccountSysNo,
		oReq.inReserve,
		oReq.errmsg,
		oReq.outReserve
    );
	oResp.errmsg = oReq.errmsg;
	oResp.outReserve = oReq.outReserve;

    return m_pSvcCntl->putSvcObj(&oResp, oResp.dwResult);
}


int CACAccountDaoService::GetACItemsByFilter(CCmdObj& oCmd)
{
	char* pBuf = NULL; uint32_t nLen = 0;
    oCmd.getBuffer(pBuf, nLen);
    CByteStreamNetwork bs(pBuf, nLen);
    bs.isStoring(false);
    bs.setVersion(oCmd.getVersion());
    CACAccountDaoGetACItemsByFilterReq oReq;
    if(!oReq.Serialize(bs))
    {
        m_sLastErrMsg = "Req.Serialize(bs) failed.";
        return -1;
    }

    CCntlInfo oCntlInfo;
    oCntlInfo.setCntl(reinterpret_cast<void*>(m_pSvcCntl));
    oCntlInfo.setOperatorUin(oCmd.getOperatorUin());
	oCntlInfo.setOperatorClientIP(oCmd.getOperatorClientIP());
	oCntlInfo.setOperatorKey(oCmd.getOperatorKey());
    oCntlInfo.setRouteKey(oCmd.getUin());
    oCntlInfo.setCmdId(oCmd.getCmdId());
    oCntlInfo.setNetioClientIP(oCmd.getNetioClientIP());
	oCntlInfo.setSerialNo(oCmd.getSerialNo());
	oCntlInfo.setPkgTimestamp(oCmd.getPkgTimestamp());
	
	if (oCmd.getVersion() == 3)
    {
        CCmdObjExt& oCmdExt = dynamic_cast<CCmdObjExt&>(oCmd);
        oCntlInfo.setAppId(oCmdExt.getReserve1());
        oCntlInfo.setDomainId(oCmdExt.getReserve2());
    }
	
	CACAccountDaoGetACItemsByFilterResp oResp;
	
    oResp.dwResult = m_pSvc->GetACItemsByFilter(
		oCntlInfo,
		oReq.machineKey,
		oReq.source,
		oReq.sceneId,
		oReq.bankAccountFilter,
		oReq.inReserve,
		oReq.errmsg,
		oReq.aCItems,
		oReq.outReserve
    );
	oResp.errmsg = oReq.errmsg;
	oResp.aCItems = oReq.aCItems;
	oResp.outReserve = oReq.outReserve;

    return m_pSvcCntl->putSvcObj(&oResp, oResp.dwResult);
}


int CACAccountDaoService::GetACItemsBySysNos(CCmdObj& oCmd)
{
	char* pBuf = NULL; uint32_t nLen = 0;
    oCmd.getBuffer(pBuf, nLen);
    CByteStreamNetwork bs(pBuf, nLen);
    bs.isStoring(false);
    bs.setVersion(oCmd.getVersion());
    CACAccountDaoGetACItemsBySysNosReq oReq;
    if(!oReq.Serialize(bs))
    {
        m_sLastErrMsg = "Req.Serialize(bs) failed.";
        return -1;
    }

    CCntlInfo oCntlInfo;
    oCntlInfo.setCntl(reinterpret_cast<void*>(m_pSvcCntl));
    oCntlInfo.setOperatorUin(oCmd.getOperatorUin());
	oCntlInfo.setOperatorClientIP(oCmd.getOperatorClientIP());
	oCntlInfo.setOperatorKey(oCmd.getOperatorKey());
    oCntlInfo.setRouteKey(oCmd.getUin());
    oCntlInfo.setCmdId(oCmd.getCmdId());
    oCntlInfo.setNetioClientIP(oCmd.getNetioClientIP());
	oCntlInfo.setSerialNo(oCmd.getSerialNo());
	oCntlInfo.setPkgTimestamp(oCmd.getPkgTimestamp());
	
	if (oCmd.getVersion() == 3)
    {
        CCmdObjExt& oCmdExt = dynamic_cast<CCmdObjExt&>(oCmd);
        oCntlInfo.setAppId(oCmdExt.getReserve1());
        oCntlInfo.setDomainId(oCmdExt.getReserve2());
    }
	
	CACAccountDaoGetACItemsBySysNosResp oResp;
	
    oResp.dwResult = m_pSvc->GetACItemsBySysNos(
		oCntlInfo,
		oReq.machineKey,
		oReq.source,
		oReq.sceneId,
		oReq.sysNoList,
		oReq.inReserve,
		oReq.errmsg,
		oReq.aACItems,
		oReq.outReserve
    );
	oResp.errmsg = oReq.errmsg;
	oResp.aACItems = oReq.aACItems;
	oResp.outReserve = oReq.outReserve;

    return m_pSvcCntl->putSvcObj(&oResp, oResp.dwResult);
}


int CACAccountDaoService::GetBankAccountByFilter(CCmdObj& oCmd)
{
	char* pBuf = NULL; uint32_t nLen = 0;
    oCmd.getBuffer(pBuf, nLen);
    CByteStreamNetwork bs(pBuf, nLen);
    bs.isStoring(false);
    bs.setVersion(oCmd.getVersion());
    CACAccountDaoGetBankAccountByFilterReq oReq;
    if(!oReq.Serialize(bs))
    {
        m_sLastErrMsg = "Req.Serialize(bs) failed.";
        return -1;
    }

    CCntlInfo oCntlInfo;
    oCntlInfo.setCntl(reinterpret_cast<void*>(m_pSvcCntl));
    oCntlInfo.setOperatorUin(oCmd.getOperatorUin());
	oCntlInfo.setOperatorClientIP(oCmd.getOperatorClientIP());
	oCntlInfo.setOperatorKey(oCmd.getOperatorKey());
    oCntlInfo.setRouteKey(oCmd.getUin());
    oCntlInfo.setCmdId(oCmd.getCmdId());
    oCntlInfo.setNetioClientIP(oCmd.getNetioClientIP());
	oCntlInfo.setSerialNo(oCmd.getSerialNo());
	oCntlInfo.setPkgTimestamp(oCmd.getPkgTimestamp());
	
	if (oCmd.getVersion() == 3)
    {
        CCmdObjExt& oCmdExt = dynamic_cast<CCmdObjExt&>(oCmd);
        oCntlInfo.setAppId(oCmdExt.getReserve1());
        oCntlInfo.setDomainId(oCmdExt.getReserve2());
    }
	
	CACAccountDaoGetBankAccountByFilterResp oResp;
	
    oResp.dwResult = m_pSvc->GetBankAccountByFilter(
		oCntlInfo,
		oReq.machineKey,
		oReq.source,
		oReq.sceneId,
		oReq.bankAccountFilter,
		oReq.inReserve,
		oReq.errmsg,
		oReq.bankAccount,
		oReq.outReserve
    );
	oResp.errmsg = oReq.errmsg;
	oResp.bankAccount = oReq.bankAccount;
	oResp.outReserve = oReq.outReserve;

    return m_pSvcCntl->putSvcObj(&oResp, oResp.dwResult);
}


int CACAccountDaoService::GetBankAccountBySysNos(CCmdObj& oCmd)
{
	char* pBuf = NULL; uint32_t nLen = 0;
    oCmd.getBuffer(pBuf, nLen);
    CByteStreamNetwork bs(pBuf, nLen);
    bs.isStoring(false);
    bs.setVersion(oCmd.getVersion());
    CACAccountDaoGetBankAccountBySysNosReq oReq;
    if(!oReq.Serialize(bs))
    {
        m_sLastErrMsg = "Req.Serialize(bs) failed.";
        return -1;
    }

    CCntlInfo oCntlInfo;
    oCntlInfo.setCntl(reinterpret_cast<void*>(m_pSvcCntl));
    oCntlInfo.setOperatorUin(oCmd.getOperatorUin());
	oCntlInfo.setOperatorClientIP(oCmd.getOperatorClientIP());
	oCntlInfo.setOperatorKey(oCmd.getOperatorKey());
    oCntlInfo.setRouteKey(oCmd.getUin());
    oCntlInfo.setCmdId(oCmd.getCmdId());
    oCntlInfo.setNetioClientIP(oCmd.getNetioClientIP());
	oCntlInfo.setSerialNo(oCmd.getSerialNo());
	oCntlInfo.setPkgTimestamp(oCmd.getPkgTimestamp());
	
	if (oCmd.getVersion() == 3)
    {
        CCmdObjExt& oCmdExt = dynamic_cast<CCmdObjExt&>(oCmd);
        oCntlInfo.setAppId(oCmdExt.getReserve1());
        oCntlInfo.setDomainId(oCmdExt.getReserve2());
    }
	
	CACAccountDaoGetBankAccountBySysNosResp oResp;
	
    oResp.dwResult = m_pSvc->GetBankAccountBySysNos(
		oCntlInfo,
		oReq.machineKey,
		oReq.source,
		oReq.sceneId,
		oReq.sysNoList,
		oReq.inReserve,
		oReq.errmsg,
		oReq.bankAccount,
		oReq.outReserve
    );
	oResp.errmsg = oReq.errmsg;
	oResp.bankAccount = oReq.bankAccount;
	oResp.outReserve = oReq.outReserve;

    return m_pSvcCntl->putSvcObj(&oResp, oResp.dwResult);
}


#include "svc_main.h"

IServiceFactory* GetServiceFactory(void)
{
    return CSvcFactorySingleton::Instance();
}
